<template>
  <div class="login_container">
    <div class="login_main">
      <img :src="logo" class="login_main_logo" />
      <div class="login_main_title">{{ mainTitle }}</div>
      <div class="login_main_title" data-type="en">{{ mainTitleEn }}</div>
    </div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login_form"
      autocomplete="on"
      label-position="left"
    >
      <div class="login_form_title">{{ formTitle }}</div>
      <el-form-item prop="username">
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>
      <el-tooltip
        v-model="capsTooltip"
        content="大小写锁定键打开"
        placement="right"
        manual
      >
        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
        </el-form-item>
      </el-tooltip>
      <el-button
        :loading="loading"
        type="primary"
        class="login_btn"
        @click.native.prevent="handleLogin"
        >{{ $t("system.logIn") }}</el-button
      >
      <div class="browserTips" v-if="!isChrome()">推荐使用chrome浏览器，<a href="https://www.google.cn/chrome/" target="_blank">点击这里下载</a>。查看<span @click="showBrowserTip=true">更多浏览器</span>设置帮助</div>
      <el-dialog  class="browserTipsDialog" :visible.sync="showBrowserTip">
        <p>使用360或QQ浏览器请设置为<span>极速模式</span>：</p>
        <p>点击查看设置方法：<a href="https://browser.qq.com/faq/#/detail/36" target="_blank">QQ浏览器设置</a>、<a href="http://se.360.cn/v6/help/help5.html" target="_blank">360浏览器设置</a></p>
      </el-dialog>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import SocialSign from "./components/SocialSignin";
import settings from "@/settings";
export default {
  name: "Login",
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请填写用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error("密码不得小于4位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: process.env.NODE_ENV == "production" ? null : "admin",
        password: process.env.NODE_ENV == "production" ? null : "9999",
      },
      loginRules: {
        username: [{ required: true, trigger: "blur", validator: validateUsername }],
        password: [{ required: true, trigger: "blur", validator: validatePassword }],
      },
      logo: require("@/assets/login/applogo.png"),
      formTitle: "看看医疗药店管理系统",
      mainTitle: "看看医疗-药者用心、医者仁心",
      mainTitleEn:
        "Look at the Healthcare - the Heart of the Drug,the Heart of the Doctor",
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {},
      showBrowserTip:false,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
    console.log(process.env.NODE_ENV);
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if (
          (shiftKey && key >= "a" && key <= "z") ||
          (!shiftKey && key >= "A" && key <= "Z")
        ) {
          this.capsTooltip = true;
        } else {
          this.capsTooltip = false;
        }
      }
      if (key === "CapsLock" && this.capsTooltip === true) {
        this.capsTooltip = false;
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery,
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
    isChrome(){
      if(navigator.userAgent.toLowerCase().indexOf("chrome")!=-1){
        return true;
      }else{
        return false
      }
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #fff;
$formColor: #7e99b9;
$titleColor: #456488;
$btnColor: #456488;
$cursor: #fff;

@function px2vw($px) {
  @return $px/1920 * 100vw;
}

@supports (-webkit-mask: none) and (not (cater-color: $formColor)) {
  .login_container .el-input input {
    color: $formColor;
  }
}
.login_container {
  min-height: 100%;
  width: 100%;
  background: url("~@/assets/login/bg.png") $bg no-repeat center center;
  background-size: cover;
  overflow: hidden;
  position: relative;
  display: flex;
  align-items: center;
  &:after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: px2vw(347);
    height: px2vw(325);
    background: url("~@/assets/login/icon1.png") no-repeat center center;
    background-size: 100% 100%;
  }
  .el-input {
    display: inline-block;
    height: px2vw(80);
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 0px;
      color: $formColor;
      height: px2vw(80);
      caret-color: $formColor;
      font-size: px2vw(24);
      letter-spacing: 3px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px transparent inset !important;
        -webkit-text-fill-color: $formColor !important;
      }
      &::-webkit-input-placeholder {
        color: $formColor;
      }
    }
  }
  .el-form-item {
    border-bottom: 1px solid #cfcfcf;
    margin-bottom: px2vw(50);
  }
  .login_main {
    text-align: center;
    width: px2vw(1110);
    height: px2vw(370);
    color: $cursor;
    font-size: px2vw(42);
    letter-spacing: 5px;
    font-weight: bold;
    .login_main_logo {
      height: px2vw(181);
      width: px2vw(181);
      margin-bottom: px2vw(35);
    }
    .login_main_title {
      font-size: px2vw(46);
      color: $titleColor;
      letter-spacing: 5px;
      height: px2vw(80);
      line-height: px2vw(80);
    }
    .login_main_title[data-type="en"] {
      font-size: px2vw(16);
      letter-spacing: 2px;
      height: px2vw(44);
      line-height: px2vw(44);
      font-weight: normal;
    }
  }
  .login_form {
    width: px2vw(790);
    height: 100vh;
    padding: px2vw(200) px2vw(100) px2vw(100);
    overflow: hidden;
    background-color: rgba(255, 255, 255, 0.6);
    border-radius: px2vw(18);
    margin: auto;
    position: relative;
    &:after {
      content: "";
      position: absolute;
      height: px2vw(640);
      width: px2vw(640);
      border-radius: 50%;
      background-color: rgba(152, 175, 198, 0.1);
      top: px2vw(-300);
      right: px2vw(-280);
    }
    &:before {
      content: "";
      position: absolute;
      height: px2vw(640);
      width: px2vw(640);
      border-radius: 50%;
      background-color: rgba(147, 196, 188, 0.6);
      bottom: px2vw(-450);
      margin: auto;
      left: 0;
      right: 0;
    }
    .login_form_title {
      font-size: px2vw(36);
      color: $formColor;
      text-align: center;
      font-weight: bold;
      height: px2vw(150);
      line-height: px2vw(150);
      letter-spacing: 5px;
    }
    .login_btn {
      width: px2vw(300);
      height: px2vw(60);
      line-height: px2vw(60);
      background-color: $btnColor;
      padding: 0;
      margin: px2vw(80) auto 0;
      display: block;
      font-size: px2vw(26);
      border-radius: px2vw(30);
      border: none;
      letter-spacing: 5px;
      position: relative;
      z-index: 2;
    }
    .browserTips{
      line-height: px2vw(100);
      font-size: px2vw(16);
      text-align: center;
      color:#666;
      a,span{
        cursor: pointer;
        color:#1890ff;
        &:hover{
          color:#40a9ff;
        }
      }
    }
    .browserTipsDialog{
      p{
        text-align: left;
        span{
          color: #f30;
        }
      }
    }
  }
}
</style>
